package Control;

import Database.DBCustomer;
import Database.DBSalesOrder;
import Model.Customer;
import Model.SalesOrder;

public class OrderCtrl {
	SalesOrder salesOrder;
	DBCustomer customerDB = new DBCustomer();
    DBSalesOrder order = new DBSalesOrder();

    public String createOrder(String sDate, int amount, String deliveryStatus, String deliveryDate, int orderID, int customerID)
    {
	String message = null;
	if (sDate != null && amount != 0 && deliveryStatus != null && deliveryDate != null && orderID != 0 && customerID != 0) 
	{

	    Customer customerObj = customerDB.findCustomer(customerID);
	    //System.out.println(customerObj.getName());  customer object works.
	    if(customerObj != null){
		
		
	    
	    if (order.findSalesOrder(orderID).getOrderID() != orderID) {
	    	salesOrder = new SalesOrder(orderID, sDate, amount, deliveryStatus, deliveryDate, customerID);

		try {
			order.insertSalesOrder(salesOrder);
		    message = "SalesOrder: " + orderID + " was created succesfuly!";
		} catch (Exception e) {

		    e.printStackTrace();
		}

	    } else {
		// Sent a message to the user that the input was wrong
		message = "SalesOrder with this ID[" + orderID + "] already exist!";
	    }
	} else {
	    message = "Please fill up all fields!";

	}
	}else {
	    
	    message = "Customer doesn't exisit";
	}
	return message;
    }
    
    
    public String findSalesOrder(int orderID) {
    	SalesOrder salesOrder = null;
    	String message = null;
    	try {
    		salesOrder = order.findSalesOrder(orderID);
    	    System.out.println(salesOrder.getOrderID());
    	} catch (Exception e) {

    	    System.out.println("can't find");
    	}
    	if (salesOrder.getOrderID() == 0) { // id 0 must not exist

    	    message = "Can't find order!";
    	} else {

    	    message = "Sales Order [ID - " + salesOrder.getOrderID() + "]   [Customer - " + salesOrder.getCustomer() + "]    [Deadline - " + salesOrder.getDeliveryDate() + "]";
    	}
    	// Sent a message to the user that the input was wrong or the customer
    	// was not found
    	return message;

    }
    public String updateSalesOrder(String sDate, int amount, String deliveryStatus, String deliveryDate, int orderID, int cusotmerID) 
    {
    	String message = null;
    	if (order.findSalesOrder(orderID).getOrderID() == orderID) {
			salesOrder = new SalesOrder(orderID, sDate, amount, deliveryStatus, deliveryDate, cusotmerID);
    	    order.updateSalesOrder(salesOrder);
    	    message = orderID + " was updated succesfuly!";
    	} else {

    	    message = "Customer doesn't exist!";
    	}
    	// We should either prombt for what information to update
    	// or if we should force an update in all fields of the customer

    	return message;
        }
    public String deleteSalesOrder(int orderID) {

	String message = null;
	if (order.findSalesOrder(orderID).getOrderID() == orderID) {
	    int id = order.findSalesOrder(orderID).getOrderID();
	    order.deleteSalesOrder(orderID);

	    message = id + " order was deleted succesfuly!";
	} else {

	    message = "Order doesn't exist!";
	}
	// Here we need a script that removes the information
	// from the database

	return message;
    }

}


